package com.jiuyun.servlet;

import com.jiuyun.dao.StudentaDao;
import com.jiuyun.dao.impi.StudentaDaoimpl;
import com.jiuyun.entity.Studenta;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/user/*")
public class HzcServlet extends BaseServlet {
    StudentaDao dao=new StudentaDaoimpl();

    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List list=null;
        try {
            list=dao.getAll();
            System.out.println(list);
            request.getSession().setAttribute("list",list);
        } catch (SQLException throwables){
            throwables.printStackTrace();
        }
        response.sendRedirect("/Eventstar/houtai/user.jsp");
        //request.getRequestDispatcher("/houtai/user.jsp").forward(request,response);
    }
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("用户添加");
        int cid=Integer.parseInt(request.getParameter("classid"));
        String name=request.getParameter("username");
        String pass=request.getParameter("passWord");
        String sex=request.getParameter("sex");
        Studenta stu=new Studenta(cid,name,pass,sex);
        System.out.println(stu);
        int a=0;
        try {
            a=dao.getadd(stu);
            System.out.println(a);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(a>0){
            System.out.println("添加成功！");
        }else{
            System.out.println("添加失败！");
        }
        //request.getRequestDispatcher("/user/find").forward(request,response);
        response.sendRedirect("user/find");
    }
    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("用户删除");
        int id=Integer.parseInt(request.getParameter("userid"));
        int ok=0;
        try {
            ok=dao.getdel(id);
            System.out.println(ok);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //request.getRequestDispatcher("/user/find").forward(request,response);
        response.sendRedirect("user/find");
    }
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("商品修改");
            int cid=Integer.parseInt(request.getParameter("cid"));
            String name=request.getParameter("name");
            String pass=request.getParameter("pass");
            String sex=request.getParameter("sex");
            int id=Integer.parseInt(request.getParameter("id"));
        System.out.println(sex);
        try {
            dao.update(cid,name,pass,sex,id);
            System.out.println("来了");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //request.getRequestDispatcher("/user/find").forward(request,response);
        response.sendRedirect("user/find");
    }
    protected void xiugai(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        int gid=Integer.parseInt(request.getParameter("gid"));
        //System.out.println(request.getParameter("gid"));
        List<Studenta> list=dao.getatAll(gid);
        System.out.println(list);
//        for (Student S: list){
//            int id=S.getStid();
//            int cid=S.getClassid();
//            String name=S.getStname();
//            String pass=S.getStpass();
//            String sex=S.getStsex();
//
//            request.setAttribute("id", id);
//            request.setAttribute("cid", cid);
//            request.setAttribute("name", name);
//            request.setAttribute("pass", pass);
//            request.setAttribute("sex", sex);
//        }
        HttpSession session=request.getSession();
        session.setAttribute("userlist",list);
        response.sendRedirect("/Eventstar/houtai/xiugai.jsp");
        //request.getRequestDispatcher("/houtai/xiugai.jsp").forward(request, response);
    }
}
